<?php

namespace app\admin\controller;

use app\admin\validate\SBanner as SBannerValidate;
use app\common\business\SPlatform as SPlatformBus;
use app\common\lib\Check;
use app\common\lib\toCamelCase;
use app\common\model\mysql\SRubikImg;

class SPlatform extends AuthBase
{
    /**************** 轮播图管理 **********************/
    public function bannerList(){
        $where = [];
        // 获取分页参数
        $page = $this->request->param('pageNum', 1);
        $limit = $this->request->param('pageSize', 10);
        $bannerShow = $this->request->get("bannerShow","");
        if ($bannerShow !== null && $bannerShow !== '') {
            $where[] = ["banner_show", "=", $bannerShow];
        }
        $where[] = ["del_flag", "=", '0'];
        try {
            $menuData = (new SPlatformBus())->getNormalBanner($where,'*',$limit,$page);
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
            if($menuData['data']){
                foreach ($menuData['data'] as $k=>$v){
                    $menuData['data'][$k]['bannerImg'] = (new Check())->checkUrl($v['bannerImg']);
                }
            }
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"轮播图列表",$menuData);
    }

    //轮播图添加
    public function bannerAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = (new toCamelCase())->toCamelCaseArray($allParams,'1',2);
        $validate = new SBannerValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new SPlatformBus())->addBanner($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //上传轮播图图片
    public function bannerUploadFile(){
        $imgUrl = file::uploadFile('file','bannerImg');
        return show(config("status.success"),"ok",$imgUrl);
    }

    //轮播图修改
    public function bannerEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            // 假设checkUrl是一个静态方法，它返回需要检查的URL前缀（如"com/"）
            $urlPrefix = (new Check())->checkUrlPrefix();
            // 检查字符串是否以特定前缀开头
            if (strpos($data['banner_img'], $urlPrefix) === 0) {
                // 直接使用前缀的长度来截取字符串
                $data['banner_img'] = substr($data['banner_img'], strlen($urlPrefix));
            }
            $result = (new SPlatformBus())->updateBannerById($data['banner_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //删除轮播图
    public function bannerDel(){
        $banner = $this->request->param("banner","","trim");
        if(empty($banner)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SPlatformBus())->delBannerIds($banner);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }

    //轮播图详情
    public function bannerDetails(){
        $banner = $this->request->param("banner","","trim");
        if(empty($banner)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $configData = (new SPlatformBus())->getBannerById($banner);
            $configData = (new toCamelCase())->toCamelCaseArray($configData);
            $configData['bannerImg'] = (new Check())->checkUrl($configData['bannerImg']);
        }catch (\Exception $e){
            $configData = [];
        }
        return show(config("status.success"),"轮播图详情",$configData);
    }

    //轮播图选择链接
    public function chooseAddressLink(){
        $index = $this->request->param("index","","trim");
        if(empty($index)){
            return show(config("status.error"),"参数不能为空");
        }
        $tablesData = [];
        if($index === '1-1'){
            $tablesData = array(
                array('id'=>1, 'name'=>'饰品模板', 'url'=>'/pages/annex/special/index?id=1',),
                array('id'=>2, 'name'=>'相机模板', 'url'=>'/pages/annex/special/index?id=2',),
            );
        }elseif ($index === '1-2'){
            $tablesData = array(
                array('name'=>'基础链接', 'items'=>array(
                        array('id'=>1, 'name'=>'商城首页', 'url'=>'#',),
                        array('id'=>2, 'name'=>'商城分类', 'url'=>'#',),
                        array('id'=>3, 'name'=>'个人中心', 'url'=>'#',),
                    ),
                ),
                array('name'=>'个人中心', 'items'=>array(
                        array('id'=>4, 'name'=>'付费会员', 'url'=>'#',),
                        array('id'=>5, 'name'=>'收银页面', 'url'=>'#',),
                        array('id'=>6, 'name'=>'个人资料', 'url'=>'#',),
                    ),
                ),
                array('name'=>'分销', 'items'=>array(
                        array('id'=>22, 'name'=>'推广人列表', 'url'=>'#',),
                        array('id'=>23, 'name'=>'分销海报', 'url'=>'#',),
                        array('id'=>34, 'name'=>'我的推广', 'url'=>'#',),
                    ),
                ),
            );
        }elseif ($index === '1-3'){
            $tablesData = array(
                array('name'=>'优惠券', 'items'=>array(
                        array('id'=>1, 'name'=>'我的优惠券', 'url'=>'#',),
                        array('id'=>2, 'name'=>'优惠券列表', 'url'=>'#',),
                        array('id'=>3, 'name'=>'预售列表', 'url'=>'#',),
                    ),
                ),
                array('name'=>'拼团', 'items'=>array(
                        array('id'=>4, 'name'=>'拼团列表', 'url'=>'#',),
                    ),
                ),
                array('name'=>'秒杀', 'items'=>array(
                        array('id'=>22, 'name'=>'秒杀列表', 'url'=>'#',),
                    ),
                ),
            );
        }elseif ($index === '3-1'){
            //文章
            //$tablesData = [];
        }

        return show(config("status.success"),"链接数据",$tablesData);
    }

    /********************* 魔方管理模块 **************************************/
    public function rubikList(){
        $where = [];
        // 获取分页参数
        $page = $this->request->param('pageNum', 1);
        $limit = $this->request->param('pageSize', 10);
        $where[] = ["del_flag", "=", '0'];
        try {
            $menuData = (new SPlatformBus())->getNormalRubik($where,'*',$limit,$page);
            $menuData = (new toCamelCase())->toCamelCaseArray($menuData);
            if($menuData['data']){
                foreach ($menuData['data'] as $k=>$v){
                    $menuData['data'][$k]['rubikImg'] = (new Check())->checkUrl($v['rubikImg']);
                }
            }
        }catch (\Exception $e){
            $menuData = [];
        }
        return show(config("status.success"),"魔方列表",$menuData);
    }

    //魔方添加
    public function rubikAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $allParams = request()->param();
        $data = (new toCamelCase())->toCamelCaseArray($allParams,'1',2);
        try {
            foreach ($data['set_rubik_data'] as $v){
                if(empty($v['imgUrl'])){
                    return show(config('status.error'),"图片不能为空");
                }
                if(empty($v['url'])){
                    return show(config('status.error'),"链接不能为空");
                }
            }
            $result = (new SPlatformBus())->addRubik($data);
            (new SRubikImg())->insertAllSRubikImg($data['set_rubik_data'],$result);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //上传魔方图片
    public function rubikUploadFile(){
        $imgUrl = file::uploadFile('file','rubikImg');
        return show(config("status.success"),"ok",$imgUrl);
    }

    //魔方修改
    public function rubikEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            // 假设checkUrl是一个静态方法，它返回需要检查的URL前缀（如"com/"）
            $urlPrefix = (new Check())->checkUrlPrefix();
            $rubikImg = [];
            foreach ($data['set_rubik_data'] as $k=>$v){
                // 检查字符串是否以特定前缀开头
                $imgUrl = $v['imgUrl'];
                if (strpos($v['imgUrl'], $urlPrefix) === 0) {
                    // 直接使用前缀的长度来截取字符串
                    $imgUrl = substr($v['imgUrl'], strlen($urlPrefix));
                }
                $rubikImg[] = [
                    'imgUrl' => $imgUrl,
                    'url' => $v['url'],
                ];
            }
            $result = (new SPlatformBus())->updateRubikById($data['rubik_id'],$data);
            (new SRubikImg())->deleteRubikImgIds($data['rubik_id']);
            (new SRubikImg())->insertAllSRubikImg($rubikImg,$data['rubik_id']);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //删除魔方
    public function rubikDel(){
        $rubik = $this->request->param("rubik","","trim");
        if(empty($rubik)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $result = (new SPlatformBus())->delRubikIds($rubik);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }

    //魔方详情
    public function rubikDetails(){
        $rubik = $this->request->param("rubik","","trim");
        if(empty($rubik)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $configData = (new SPlatformBus())->getRubikById($rubik);
            $configData = (new toCamelCase())->toCamelCaseArray($configData);
            $configData['rubikImg'] = (new Check())->checkUrl($configData['rubikImg']);
            $configData['setRubikData'] = (new SRubikImg())->getRubikImg($rubik);
            foreach ($configData['setRubikData'] as $k=>$v){
                $configData['setRubikData'][$k]['imgUrl'] = (new Check())->checkUrl($v['rubik_img_url']);
                $configData['setRubikData'][$k]['fileUrl'] = (new Check())->checkUrl($v['rubik_img_url']);
                $configData['setRubikData'][$k]['url'] = $v['rubik_link'];
            }
        }catch (\Exception $e){
            $configData = [];
        }
        return show(config("status.success"),"魔方详情",$configData);
    }
}
